<?php
namespace Home\Biz;

class PromoterBiz{
	
	public static function generateShareCode(){
		$seq = 100000 + CommonBiz::getNewSeq('share_code');
		return 'FZ'.$seq;
	}
	
	public static function generateUserId(){
		$seq = 10000000 + CommonBiz::getNewSeq('user_id');
		return 'U'.$seq;
	}
	
	public static function generatePromoterId(){
		$seq = 100000 + CommonBiz::getNewSeq('promoter_id');
		return 'PR'.$seq;
		
	}
	
	
	public static function updateInfo($promoter_en){
		//参数验证 start
		$exist_promoter_en = M('promoterInfo')->where("promoter_id='%s'", $promoter_en['promoter_id'])->find();
		
		if(empty($exist_promoter_en)){
			return '不存在的用户';
		}
		
		if(empty($promoter_en['sex'])){
			return '性别选择错误';
		}
		if(empty($promoter_en['real_name'])){
			return '真实姓名不能为空';
		}
		
		if(empty($promoter_en['alipay_id'])){
			return '支付宝账户不能为空';
		}
		if(empty($promoter_en['alipay_name'])){
			return '支付宝账户不能为空';
		}
		if(empty($promoter_en['cellphone'])){
			return '手机不能为空';
		}
		if(!StringBiz::isCellphone($promoter_en['cellphone'])){
			return '手机格式不符合';
		}
		
		if(empty($promoter_en['id_card_no'])){
			return '身份证号码不能为空';
		}
		if(!StringBiz::isIdentity($promoter_en['id_card_no'])){
			return '身份证格式不正确';
		}
		
		if($exist_promoter_en['promoter_type'] == 2){
			if(empty($promoter_en['business_license_no'])){
				return '营业执照号码不能为空';
			}
		}
		if(empty($promoter_en['district_id'])){
			return '省市区选择错误';
		}
		if(empty($promoter_en['address'])){
			return '地址不能为空';
		}
		
		if(!StringBiz::isAddress($promoter_en['address'])){
			return '地址信息不正确';
		}
		
		//校验手机号是否存在
		if($exist_promoter_en['cellphone'] != $promoter_en['cellphone']){
			$exist_cellphone_promoter_en = M('promoterInfo')->where("cellphone='%s'",$promoter_en['cellphone'])->find();
			if(!empty($exist_cellphone_promoter_en)){
				return '这个手机号码已经注册过了';
			}
		}
		
		//省市区
		$district_id = $promoter_en['district_id'];
		$lst_region_en = M('', '', 'DB_system')->query("
				SELECT * FROM province p
				INNER JOIN city c
				ON p.`province_id` = c.`province_id`
				INNER JOIN district d
				ON c.`city_id` = d.`city_id`
				WHERE district_id = '%s'
				", $district_id);
		
		
		if(empty($lst_region_en)){
			return '错误的区域选择';
		}
		else{
			$region_en = $lst_region_en[0];
		}
		//参数验证 end
		
		
		$promoter_en['province_id']		= $region_en['province_id'];
		$promoter_en['province_name']	= $region_en['province_name'];
		$promoter_en['city_id']			= $region_en['city_id'];
		$promoter_en['city_name']		= $region_en['city_name'];
		$promoter_en['district_id']		= $region_en['district_id'];
		$promoter_en['district_name']	= $region_en['district_name'];
		
		//更新promoter_info
		M('promoterInfo')->where("promoter_id='%s'", $promoter_en['promoter_id'])->save($promoter_en);
		
		//更新user_info
		$update_user_info_en = array(
				'user_name' => $promoter_en['real_name'],
				'real_name' => $promoter_en['real_name'],
				'cellphone' => $promoter_en['cellphone'],
				'sex' 		=> $promoter_en['sex'],
				'province_id'	=> $promoter_en['province_id'],
				'province_name' => $promoter_en['province_name'],
				'city_id'		=> $promoter_en['city_id'],
				'city_name' 	=> $promoter_en['city_name'],
				'district_id'	=> $promoter_en['district_id'],
				'district_name' => $promoter_en['district_name'],
				'address'		=> $promoter_en['address'],
				'update_user'	=> $promoter_en['update_user'],
				'update_date'	=> date("y-m-d H:i:s", time())
		);
		M('userInfo', '', 'DB_trade')->where("user_id='%s'", $exist_promoter_en['user_id'])->save($update_user_info_en);
	}
	
	
	public static function updatePwd($promoter_id, $old_pwd, $new_pwd, $new_pwd2){
		// 数据校验start
		if($new_pwd != $new_pwd2){
			return '两次输入的密码不一致';
		}
		
		$promoter_en = M('promoterInfo')->where("promoter_id='%s'", $promoter_id)->find();
		if(empty($promoter_en)){
			return '用户不存在';
		}
		else{
			if($promoter_en['status'] != 1){
				return '用户状态无效，不允许提现';
			}
		}
		
		//验证老密码
		$old_encrypt_password = md5(C('SystemConfig.encrypt_key').$promoter_id.$old_pwd);
		if($promoter_en['password'] != $old_encrypt_password){
			return '老密码错误';
		}
		// 数据校验end
		
		//更新密码
		$new_encrypt_password = md5(C('SystemConfig.encrypt_key').$promoter_id.$new_pwd);
		M('promoterInfo')->where("promoter_id='%s'", $promoter_id)->setField('password', $new_encrypt_password);
		
	}
	
	
	public static function reRegister($promoter_id){
		
		if(empty($promoter_id)){
			return '推广人id不存在';
		}
		$promoter_en = M('promoterInfo')->where("promoter_id='%s'", $promoter_id)->find();
		if(empty($promoter_en)){
			return '推广人不存在';
		}
		
		M('promoterInfo')->where("promoter_id='%s'", $promoter_id)->delete();
	}
	
	
	/**
	 * 
	 * @param unknown $promoter_id
	 * @param unknown $page_index
	 */
	public static function getAmountList($promoter_id, $page_index, $page_size){
		
		$lst_amount_en = M('promoterAmount')->where("promoter_id='%s'", $promoter_id)
		->page($page_index, $page_size)
		->order('create_date DESC')
		->select();

		return $lst_amount_en;
	}
	
	
	
	public static function getWithdrawList($promoter_id, $page_index, $page_size){
	
		$lst_withdraw_en = M('promoterWithdraw')->where("promoter_id='%s'", $promoter_id)
		->page($page_index, $page_size)
		->order('create_date DESC')
		->select();
		
		foreach($lst_withdraw_en as $key=>$withdraw_en){
			$lst_withdraw_en[$key]['status_text'] = StringBiz::getWithdrawStatusText($lst_withdraw_en[$key]['status']);
		}
	
		return $lst_withdraw_en;
	}
	
	/**
	 * 
	 * @param unknown $promoter_id
	 * @param unknown $withdraw_amount
	 * @param unknown $user
	 * @return string
	 */
	public static function withdraw($promoter_id, $withdraw_amount, $user){
		// 数据校验start
		if(!is_numeric($withdraw_amount)){
			return '输入了错误的金额格式';
		}
		
		if($withdraw_amount <= 0){
			return '提现金额需大于0';
		}
		
		$promoter_en = M('promoterInfo')->where("promoter_id='%s'", $promoter_id)->find();
		if(empty($promoter_en)){
			return '用户不存在';
		}
		else{
			if($promoter_en['status'] != 1){
				return '用户状态无效，不允许提现';
			}
		}
		
		if(empty($promoter_en['alipay_id']) || empty($promoter_en['alipay_name'])){
			return '请前往【个人中心】->【个人信息】中，补齐【支付宝账户】和【支付宝姓名】方可提现。';
		}
		
		$curr_amount 			= $promoter_en['amount'];
		$curr_withdraw_amount 	= $promoter_en['withdraw_amount'];
		
		if($curr_amount < $withdraw_amount){
			return '提现金额大于当前用户余额';
		}
		
		if($curr_withdraw_amount < $withdraw_amount){
			return '提现金额大于当前用户可提现余额';
		}
		// 数据校验end
		
		//新增提现申请数据
		$add_promoter_withdraw_en = array(
				'promoter_id'		=> $promoter_en['promoter_id'],
				'promoter_name' 	=> $promoter_en['promoter_name'],
				'withdraw_amount'	=> $withdraw_amount,
				'submit_date'		=> date("y-m-d H:i:s", time()),
				'status'			=> 1,
				'create_user'		=> 	$user,
    			'create_date'		=>	date("y-m-d H:i:s", time()),
		);
		M('promoterWithdraw')->add($add_promoter_withdraw_en);
		
		
		//更改用户账户余额和可提现余额，并写入明细start
		$update_promoterInfo_en = array(
				'amount'			=> $curr_amount - $withdraw_amount,
				'withdraw_amount'	=> $curr_withdraw_amount - $withdraw_amount,
		);
		M('promoterInfo')->where("promoter_id='%s'", $promoter_id)->save($update_promoterInfo_en);
		
		
		$add_promoter_amount_en = array(
				'promoter_id'	=> $promoter_en['promoter_id'],
				'promoter_name' => $promoter_en['promoter_name'],
				'amount_kind'	=> 2,
				'amount_type'	=> 22,
				'amount'		=> $withdraw_amount,
				'begin_amount'	=> $curr_amount,
				'end_amount'	=> $curr_amount - $withdraw_amount,
				'remark'		=> '提现'.$withdraw_amount.'元',
				'withdraw_flag' => 1,
				'status'		=> 1,
				'create_user'	=> 	$user,
				'create_date'	=>	date("y-m-d H:i:s", time()),
		);
		M('promoterAmount')->add($add_promoter_amount_en);
		
		$add_promoter_withdraw_amount_en = array(
				'promoter_id'			=> $promoter_en['promoter_id'],
				'promoter_name' 		=> $promoter_en['promoter_name'],
				'amount_kind'			=> 2,
				'amount_type'			=> 22,
				'withdraw_amount'		=> $withdraw_amount,
				'begin_withdraw_amount'	=> $curr_amount,
				'end_withdraw_amount'	=> $curr_withdraw_amount - $withdraw_amount,
				'remark'				=> '提现'.$withdraw_amount.'元',
				'status'				=> 1,
				'create_user'		=> 	$user,
				'create_date'		=>	date("y-m-d H:i:s", time()),
		);
		M('promoterWithdrawAmount')->add($add_promoter_withdraw_amount_en);
		//更改用户账户余额和可提现余额，并写入明细end
	}


	public static function changeSubRevenueRate($promoter_id, $revenue_rate, $user){

		//region 1、数据校验
		if(!is_numeric($revenue_rate)){
			return '输入了错误的金额格式';
		}
		
		if($revenue_rate < 0 || $revenue_rate > 0){
			return '分成比例需要大于等于0并小于等于100';
		}
		
		$promoter_en = M('promoterInfo')->where("promoter_id='%s'", $promoter_id)->find();
		if(empty($promoter_en)){
			return '用户不存在';
		}
		else{
			if($promoter_en['status'] != 1){
				return '用户状态无效，不允许修改';
			}
		}

		if($promoter_en['has_superior'] != 0){
			return '当前用户存在上级推广人，不允许修改';
		}
		//endregion 1、数据校验

		$sub_revenue_rate = $revenue_rate / 100;

		M('promoter_info')->where("promoter_id='%s'", $promoter_id)->setField('sub_revenue_rate', $sub_revenue_rate);

		M('promoter_relation')->where("promoter_id='%s'", $promoter_id)->setField('sub_revenue_rate', $sub_revenue_rate);

	}

}